package it.uniroma2.art.owlart.model.syntax.manchester;

import it.uniroma2.art.owlart.models.OWLModel;
import it.uniroma2.art.owlart.exceptions.ModelUpdateException;
import it.uniroma2.art.owlart.model.*;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;


public class ObjectIntersectionOf {
	private int numb;//numero degli oggetti che sono in and tra loro
	private Uri uriThat;
	private List<Primary> and;
	public OWLModel model;
	public ARTResource resource;

	public ObjectIntersectionOf(int number,OWLModel mod) {
		numb=number;
		uriThat=null;
		and=new LinkedList<Primary>();
		model=mod;
	}

	public ObjectIntersectionOf(OWLModel mod) {
		and=new LinkedList<Primary>();
		model=mod;
		//System.out.print("Creata intersezione ");
	}

	public int getElements() {
		return numb;
	}

	public String toString() {
	    return "intersection";
	}

	public Uri getUriThat() {
		return uriThat;
	}


	public void setUriThat(Uri uriThat) throws ModelUpdateException {
		this.uriThat = uriThat;
		Atomic thatAto= new Atomic(uriThat,model);
		and.add(new Primary(false,thatAto,model));
	}

	public void addPrimary(Primary p) {
		and.add(p);
	}

	public ARTResource computeIntersection() throws ModelUpdateException  {
		ARTBNode b=model.createBNode();
		ListIterator<Primary> andIterator=and.listIterator();
		ArrayList<ARTResource> res=new ArrayList<ARTResource>();
		if(and.size()==1) {
		    resource=andIterator.next().resource;
		    return resource;
		}
		while(andIterator.hasNext()) {
			ARTResource art=andIterator.next().resource;
			res.add(art);
		}
		model.addIntersectionOf(b.getID(), res);

		resource=b;

		return b;
	}
}
